# -*-encoding:utf-8-*-

import requests
import time 
from urllib.parse import urlencode
from copyheaders import headers_raw_to_dict
def get_params():

    url = "http://localhost:8081/_signature";

    r = requests.get( url = url )
    
    if r.status_code == 200:
        return r.json()
    
def s_toutiao( ):
    s_url = "https://www.toutiao.com/api/pc/feed/?"
    headers = headers_raw_to_dict(
        b'''
        accept: text/javascript, text/html, application/xml, text/xml, */*
        accept-encoding: gzip, deflate, br
        accept-language: zh-CN,zh;q=0.9
        content-type: application/x-www-form-urlencoded
        cookie: WEATHER_CITY=%E5%8C%97%E4%BA%AC; csrftoken=9c903367767e20cb66ff06abed69c3d0; ttcid=5c7f0cc621224eb5829dcc4a551bfd2e31; SLARDAR_WEB_ID=4d1fad85-619e-44c5-b03a-d33627c8cb54; tt_webid=6845799232130434568; s_v_web_id=verify_kc8dyqr3_fJFvTcDZ_IClB_4KDx_BC2J_N8opI3A7KyIN; tt_scid=-oAZ.vT9H-pwXsEyO79dHOSIJYJEWZ.xXtNbfFu6XJz7ywDn0SegCWdOXak3D4Yi8c19; __tasessionId=th4g45vls1593911856834; tt_webid=6845799232130434568
        referer: https://www.toutiao.com/
        user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
        x-requested-with: XMLHttpRequest
        '''
    )
    params = get_params()
    data = {
        "max_behot_time": int(time.time()),
        "category": "__all__",
        "utm_source": "toutiao",
        "widen": "1",
        "tadrequire": "true",
        "as": params["res"]["as_cp"]["as"],
        "cp": params["res"]["as_cp"]["cp"],
        "_signature":params["res"]["_signature"]
    } 
    
    r = requests.get(url = s_url + urlencode( data ),headers=headers)
    if r.status_code == 200:
        print( r.json() )
   
s_toutiao()